#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat src, temp, result;
    VideoCapture cap("test.avi");
    temp = imread("4.png");
    cvtColor(temp, temp, COLOR_BGR2GRAY);
    int tempH = temp.rows;
    int tempW = temp.cols;
    while(cap.read(src))
    {   
        cvtColor(src, src, COLOR_BGR2GRAY);
        int srcH = src.rows;
        int srcW = src.cols;
        int result_cols = src.cols-temp.rows+1;
        int result_rows = src.rows-temp.rows+1;
        result.create( result_rows, result_cols, CV_32FC1 );
        matchTemplate(src, temp, result, TM_SQDIFF);
        normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

        
        double minVal; double maxVal; Point minLoc; Point maxLoc;
        minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
        Point topLeft, bottomRight;
        topLeft = minLoc;
        bottomRight = Point(topLeft.x + temp.cols, topLeft.y + temp.rows);
        rectangle(src, topLeft, bottomRight, Scalar(0,0,255), 2, 8, 0);

        imshow("src", src);
        if(waitKey(1) == 27) break;
    }

    return EXIT_SUCCESS;
}